import time
import pytest
import allure
import TestCase

from Base.InitDriver import init_driver
from Page.CommonPage import CommonPage
from Utils.XiaoMaUtils import XiaoMaUtils


from Utils.PerfDogUtils import PerfDogUtils


@allure.feature('TestCase_12: 车队纯待机1H消耗的流量小于等于100KB	')
@allure.issue(
    'https://www.tapd.cn/56767367/sparrow/tcase/view/1156767367001062270?url_cache_key=ab49d6eabe9823050826460ca132aea4')
class TestCaseSpecial12:
    def setup_class(self):
        self.driver = init_driver()
        # 实例化页面
        # 桌面
        self.launcher_index_page = CommonPage(self.driver).get_launcher_index_page()
        self.launcher_app_list_page = CommonPage(self.driver).get_launcher_app_list_page()
        # 车队
        self.motorcade_index_page = CommonPage(self.driver).get_motorcade_index_page()

        # 实例化PerfDogUtils的工具类
        # 开始监控
        self.perfDogUtils = PerfDogUtils()
        # 输入需监控的应用名, 如"xting", "music"
        self.perfDogUtils.start_test("motorcade")

    def teardown_class(self):
        self.perfDogUtils.stop_test()
        XiaoMaUtils.close_app('motorcade')
        XiaoMaUtils.close_app('launcher')
        XiaoMaUtils.clear_data('motorcade')
        self.driver.quit()

    @allure.story('step1:点击应用页,点击车队图标')
    @pytest.mark.run(order=121)
    def test_special_12_1_click_list(self):
        try:
            self.launcher_index_page.click_app_list()
            self.launcher_app_list_page.click_appname(TestCase.MOTORCADE)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story('step2:进入车队,查看页面加载完成')
    @pytest.mark.run(order=122)
    def test_special_12_2_click_list(self):
        try:
            text1 = self.motorcade_index_page.is_display_motorcade_index()
            if text1 == True:
                text2 = self.motorcade_index_page.get_ele_text(self.motorcade_index_page.get_index_tip())
                if text2:
                    print("首页加载成功")
                else:
                    print("请检查网络是否正常")
            else:
                print("点击应用出现问题,无法进入")
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story('step3:等待一个小时,查看流量消耗')
    @pytest.mark.run(order=123)
    def test_special_12_3_wait(self):
        try:
            print("当前正在测试流量,请不要结束进程和更改页面")
            time.sleep(15)
            # time.sleep(3600)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

if __name__ == 'main__':
    # pass
    pytest.main("-s testcase_special_motorcode_12.py")
